package com.gagakuai.bpm.service.camunda.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;



import com.gagakuai.bpm.common.constants.BpmMetaConstants;
import com.gagakuai.bpm.dao.mapper.camunda.WorkFlowInstenceMapper;
import com.gagakuai.bpm.dao.mapper.camunda.WorkFlowTaskNodeMapper;
import com.gagakuai.bpm.domian.entity.camuda.ActHiActinst;
import com.gagakuai.bpm.domian.entity.camuda.WorkFlowInstence;
import com.gagakuai.bpm.domian.entity.camuda.WorkFlowTaskNode;
import com.gagakuai.bpm.domian.query.camunda.WorkflowQuery;
import com.gagakuai.bpm.service.camunda.WorkFlowTaskNodeService;
import com.gagakuai.bpm.utils.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WorkFlowTaskNodeServiceImpl implements WorkFlowTaskNodeService {

    @Resource
    private WorkFlowTaskNodeMapper workFlowTaskNodeMapper;

    @Resource
    private WorkFlowInstenceMapper workFlowInstenceMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return workFlowTaskNodeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(WorkFlowTaskNode record) {
        return workFlowTaskNodeMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(WorkFlowTaskNode record) {
        return workFlowTaskNodeMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(WorkFlowTaskNode record) {
        return workFlowTaskNodeMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(WorkFlowTaskNode record) {
        return workFlowTaskNodeMapper.insertSelective(record);
    }

    @Override
    public WorkFlowTaskNode selectByPrimaryKey(String id) {
        return workFlowTaskNodeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(WorkFlowTaskNode record) {
        return workFlowTaskNodeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(WorkFlowTaskNode record) {
        return workFlowTaskNodeMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<WorkFlowTaskNode> list) {
        return workFlowTaskNodeMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<WorkFlowTaskNode> list) {
        return workFlowTaskNodeMapper.batchInsert(list);
    }

    /*
     * @param processId 流程Id
     * @return {@link IPage<  WorkFlowTaskNode >}
     * @Author houhong
     * @Description //TODO 根据流程Id 获取流程办理轨迹
     * @Date 3:25 下午 2022/3/22
     */
    @Override
    public IPage<ActHiActinst> getTraceByProcesssIdPage(WorkflowQuery queryDto,
                                                        String processId) throws Exception {


        WorkFlowInstence processInstance = workFlowInstenceMapper.selectByPrimaryKey(processId);

        if (BpmMetaConstants.PROCE_IINSTANCE_ACTIVE.equals(processInstance.getState())) {

            IPage<ActHiActinst> page = new Page<>(queryDto.getPageNumber(), queryDto.getPageSize());
            IPage<ActHiActinst> actHiActinstIPage = workFlowTaskNodeMapper.selectWorkflowNoByProcessIdPage(page, queryDto, processId);

            if (actHiActinstIPage != null && actHiActinstIPage.getRecords() != null) {

                //只翻译第一页
                Long currentPage = page.getCurrent();

                if(ObjectUtils.notNull(currentPage) && currentPage.equals(1L)){
                    //首节点
                    ActHiActinst actHiActinst = actHiActinstIPage.getRecords().get(0);
                    WorkFlowInstence workFlowInstence = workFlowInstenceMapper.selectByPrimaryKey(actHiActinst.getProcInstId());
                    //流程发起人
                    String startUserId = workFlowInstence.getStartUserId();
                    actHiActinst.setOwner(startUserId);
                    actHiActinst.setAssignee(startUserId);
                }
            }
            return actHiActinstIPage;
        } else {
            IPage<ActHiActinst> page = new Page<>(queryDto.getPageNumber(), queryDto.getPageSize());
            IPage<ActHiActinst> actHiActinstIPage = workFlowTaskNodeMapper.selectWorkflowNoByProcessOorderByEndIdPage(page, queryDto, processId);

            if (actHiActinstIPage != null && actHiActinstIPage.getRecords() != null) {

                //只翻译第一页
                Long currentPage = page.getCurrent();

                if(ObjectUtils.notNull(currentPage) && currentPage.equals(1L)){
                    //首节点
                    ActHiActinst actHiActinst = actHiActinstIPage.getRecords().get(0);
                    WorkFlowInstence workFlowInstence = workFlowInstenceMapper.selectByPrimaryKey(actHiActinst.getProcInstId());
                    //流程发起人
                    String startUserId = workFlowInstence.getStartUserId();
                    actHiActinst.setOwner(startUserId);
                    actHiActinst.setAssignee(startUserId);
                }
            }
            return actHiActinstIPage;
        }


    }

    /*
     * @param processId
     * @param taskId
     * @param userId
     * @return {@link Integer}
     * @Author houhong
     * @Description //TODO 获取活跃数目
     * @Date 2:25 下午 2022/4/29
     * @Param * @param processId
     */
    @Override
    public Integer getRuTaskNumByuserName(String processId, String taskId, String userId) {

        return workFlowTaskNodeMapper.selectRuTaskNum(processId, taskId, userId);
    }

    /*
     * @param processId
     * @param userId
     * @param taskDefkey
     * @return {@link Integer}
     * @Author houhong
     * @Description //TODO 获取taskId
     * @Date 2:25 下午 2022/4/29
     * @Param processId
     */
    @Override
    public String getTaskIdByUserAndProcessIn(String processId, String userId) {

        return workFlowTaskNodeMapper.selectTaskIdByUserAndProcessIn(processId, userId);
    }

    /*
     * @param processId
     * @return {@link Integer}
     * @Author houhong
     * @Description //TODO 获取流程状态
     * @Date 2:25 下午 2022/4/29
     * @Param processId
     */
    @Override
    public String getProcessStatus(String processId) {

        WorkFlowInstence workFlowInstence = workFlowInstenceMapper.selectByPrimaryKey(processId);

        if (ObjectUtils.notNull(workFlowInstence)) {

            return workFlowInstence.getState();
        }
        return null;
    }


}

